﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml;
namespace FetionWebMessenger.Util
{
    public class WeatherXML
    {
        static public Boolean WriteXML(List<WeatherCon> weatherList)
        {
            if (weatherList == null)
                return true;
            try
            {
                String XML = "<?xml version='1.0' encoding='UTF-8'?><WeatherProvider>";
                foreach (WeatherCon contacter in weatherList)
                {
                    XML += "<subscriber  name='" + contacter.uri + "'>";
                    XML += contacter.city + "</subscriber>";
                }
                XML += "</WeatherProvider>";
                string strpath = "c:\\WeatherProvider.xml";
                StreamWriter sw = new StreamWriter(strpath);
                sw.Write(XML);
                sw.Close();
            }
            catch (System.Exception ex)
            {
                ex.ToString();
                return false;
            }
            return true;
        }
        static public List<WeatherCon> ReadXML()
        {
            string strpath = "c:\\WeatherProvider.xml";
            List<WeatherCon> list = new List<WeatherCon>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(strpath);
            XmlNode xn = xmlDoc.SelectSingleNode("WeatherProvider");
            XmlNodeList xnl = xn.ChildNodes;
            foreach (XmlNode xnf in xnl)
            {
                WeatherCon con = new WeatherCon();
                XmlElement xe = (XmlElement)xnf;
                con.uri = xe.GetAttribute("name");
                con.city = xe.InnerText;
                list.Add(con);
            }
            return list;
        }
    }
    public class WeatherCon
    {
        private String Uri;
        public System.String uri
        {
            get { return Uri; }
            set { Uri = value; }
        }
        private String City;
        public String city
        {
            get { return City; }
            set { City = value; }
        }
    }
}
